package factory;


import com.almasb.fxgl.animation.Animation;
import com.almasb.fxgl.dsl.EntityBuilder;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.*;
import com.almasb.fxgl.entity.Spawns;
import comgame.components.AnimationComponent;
import comgame.components.AttributeComponent;
import comgame.components.HealthComponent;
import comgame.entities.Enemy;
import comgame.entities.EntityType;
import com.almasb.fxgl.entity.components.CollidableComponent;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import javafx.scene.paint.Color;
import javafx.scene.effect.ColorAdjust;
import javafx.util.Duration;


public class EnemyFactory implements EntityFactory
{
    @Spawns("slime")
    public Entity newSlime(SpawnData data)
    {
        AnimationChannel animationChannel = new AnimationChannel(FXGL.image("Monster_Slime.png"), Duration.seconds(0.8), 4);
        AnimatedTexture animatedTexture = new AnimatedTexture(animationChannel);
        animatedTexture.loop();

        // 应用蓝色滤镜
        ColorAdjust blueFilter = new ColorAdjust();
        blueFilter.setHue(0.5); // 更深的蓝色色调
        blueFilter.setSaturation(0.5); // 最大饱和度
        blueFilter.setBrightness(0.0); // 保持原始亮度
        animatedTexture.setEffect(blueFilter);

        // 设置缩放比例让slime变大
        animatedTexture.setScaleX(2); // 宽度放大2倍
        animatedTexture.setScaleY(2); // 高度放大2倍

        Entity SlimeEntity = FXGL.entityBuilder()
                .at(data.getX(), data.getY())
                .type(EntityType.ENEMY)
                .viewWithBBox(animatedTexture)
                .with(new AttributeComponent())
                .with(new AnimationComponent())
                .with(new CollidableComponent(true))
                .with(new HealthComponent())
                .build();

        Enemy Slime = new Enemy(SlimeEntity);
        SlimeEntity.setProperty("Slime", Slime);

        return SlimeEntity;
    }

    @Spawns("bug")
    public Entity newBug(SpawnData data)
    {
        AnimationChannel animationChannel = new AnimationChannel(FXGL.image("Monster_Bug.png",256,64), Duration.seconds(0.8), 4);
        AnimatedTexture animatedTexture = new AnimatedTexture(animationChannel);
        animatedTexture.loop();


        Entity BugEntity = FXGL.entityBuilder()
                .at(data.getX(), data.getY())
                .type(EntityType.ENEMY)
                .viewWithBBox(animatedTexture)
                .with(new AttributeComponent())
                .with(new AnimationComponent())
                .with(new CollidableComponent(true))
                .with(new HealthComponent())
                .build();

        Enemy Bug = new Enemy(BugEntity);
        BugEntity.setProperty("Bug", Bug);

        return BugEntity;
    }

    @Spawns("bulbapedia")
    public Entity newBulbapedia(SpawnData data)
    {
        // 使用GIF动画，循环播放
        AnimationChannel animationChannel = new AnimationChannel(FXGL.image("Monster_Bulbapedia.gif",105,99), Duration.seconds(1.0), 1);
        AnimatedTexture animatedTexture = new AnimatedTexture(animationChannel);
        animatedTexture.loop();


        Entity BulbapediaEntity = FXGL.entityBuilder()
                .at(data.getX(), data.getY())
                .type(EntityType.ENEMY)
                .viewWithBBox(animatedTexture)
                .with(new AttributeComponent())
                .with(new AnimationComponent())
                .with(new CollidableComponent(true))
                .with(new HealthComponent())
                .build();

        Enemy Bulbapedia = new Enemy(BulbapediaEntity);
        BulbapediaEntity.setProperty("Bulbapedia", Bulbapedia);

        return BulbapediaEntity;
    }

    @Spawns("ghost")
    public Entity newGhost(SpawnData data)
    {
        // 使用Ghost图片
        AnimationChannel animationChannel = new AnimationChannel(FXGL.image("Monster_Ghost.png",256,96), Duration.seconds(0.8), 4);
        AnimatedTexture animatedTexture = new AnimatedTexture(animationChannel);
        animatedTexture.loop();

        Entity GhostEntity = FXGL.entityBuilder()
                .at(data.getX(), data.getY())
                .type(EntityType.ENEMY)
                .viewWithBBox(animatedTexture)
                .with(new AttributeComponent())
                .with(new AnimationComponent())
                .with(new CollidableComponent(true))
                .with(new HealthComponent())
                .build();

        Enemy Ghost = new Enemy(GhostEntity);
        GhostEntity.setProperty("Ghost", Ghost);

        return GhostEntity;
    }

    @Spawns("vegetabler")
    public Entity newVegetabler(SpawnData data)
    {
        // 使用Vegetabler图片
        AnimationChannel animationChannel = new AnimationChannel(FXGL.image("Monster_Vegetabler.png",256,96), Duration.seconds(0.8), 4);
        AnimatedTexture animatedTexture = new AnimatedTexture(animationChannel);
        animatedTexture.loop();


        Entity VegetablerEntity = FXGL.entityBuilder()
                .at(data.getX(), data.getY())
                .type(EntityType.ENEMY)
                .viewWithBBox(animatedTexture)
                .with(new AttributeComponent())
                .with(new AnimationComponent())
                .with(new CollidableComponent(true))
                .with(new HealthComponent())
                .build();

        Enemy Vegetabler = new Enemy(VegetablerEntity);
        VegetablerEntity.setProperty("Vegetabler", Vegetabler);

        return VegetablerEntity;
    }

    @Spawns("pumpking")
    public Entity newPumpking(SpawnData data)
    {
        // 使用Pumpking GIF图片
        AnimationChannel animationChannel = new AnimationChannel(FXGL.image("Monster_Pumpking.gif",170*2,187*2), Duration.seconds(1.0), 1);
        AnimatedTexture animatedTexture = new AnimatedTexture(animationChannel);
        animatedTexture.loop();

        Entity PumpkingEntity = FXGL.entityBuilder()
                .at(data.getX(), data.getY())
                .type(EntityType.ENEMY)
                .viewWithBBox(animatedTexture)
                .with(new AttributeComponent())
                .with(new AnimationComponent())
                .with(new CollidableComponent(true))
                .with(new HealthComponent())
                .build();

        Enemy Pumpking = new Enemy(PumpkingEntity);
        PumpkingEntity.setProperty("Pumpking", Pumpking);

        return PumpkingEntity;
    }

    @Spawns("everscream")
    public Entity newEverscream(SpawnData data)
    {
        // 使用Everscream GIF图片
        AnimationChannel animationChannel = new AnimationChannel(FXGL.image("Monster_Everscream.gif",170*2,143*2), Duration.seconds(1.0), 1);
        AnimatedTexture animatedTexture = new AnimatedTexture(animationChannel);
        animatedTexture.loop();

        Entity EverscreamEntity = FXGL.entityBuilder()
                .at(data.getX(), data.getY())
                .type(EntityType.ENEMY)
                .viewWithBBox(animatedTexture)
                .with(new AttributeComponent())
                .with(new AnimationComponent())
                .with(new CollidableComponent(true))
                .with(new HealthComponent())
                .build();

        Enemy Everscream = new Enemy(EverscreamEntity);
        EverscreamEntity.setProperty("Everscream", Everscream);

        return EverscreamEntity;
    }

    @Spawns("empress_of_light")
    public Entity newEmpressOfLight(SpawnData data)
    {
        // 使用Empress_of_Light GIF图片
        AnimationChannel animationChannel = new AnimationChannel(FXGL.image("Monster_Empress_of_Light.gif",170*2,96*2), Duration.seconds(1.0), 1);
        AnimatedTexture animatedTexture = new AnimatedTexture(animationChannel);
        animatedTexture.loop();

        Entity EmpressOfLightEntity = FXGL.entityBuilder()
                .at(data.getX(), data.getY())
                .type(EntityType.ENEMY)
                .viewWithBBox(animatedTexture)
                .with(new AttributeComponent())
                .with(new AnimationComponent())
                .with(new CollidableComponent(true))
                .with(new HealthComponent())
                .build();

        Enemy EmpressOfLight = new Enemy(EmpressOfLightEntity);
        EmpressOfLightEntity.setProperty("Empress_of_Light", EmpressOfLight);

        return EmpressOfLightEntity;
    }

    @Spawns("plantera")
    public Entity newPlantera(SpawnData data) {
        // 第一阶段动画
        AnimationChannel firstForm = new AnimationChannel(
            FXGL.image("Plantera_(First_form).gif", 152, 177), Duration.seconds(1.0), 1);
        // 第二阶段动画
        AnimationChannel secondForm = new AnimationChannel(
            FXGL.image("Plantera_(Second_form).gif", 152, 177), Duration.seconds(1.0), 1);

        AnimatedTexture animatedTexture = new AnimatedTexture(firstForm);
        animatedTexture.loop();
        animatedTexture.setScaleX(4.0);
        animatedTexture.setScaleY(4.0);
        animatedTexture.setRotate(-90); // 逆时针旋转90°

        AttributeComponent attr = new AttributeComponent();
        attr.setAttack(150);
        attr.setDefense(120); // 第一阶段高防御
        attr.setCritRate(0.5);
        attr.setSpeed(1.0);

        Entity planteraEntity = FXGL.entityBuilder()
                .at(data.getX(), data.getY())
                .type(EntityType.ENEMY)
                .viewWithBBox(animatedTexture)
                .with(attr)
                .with(new AnimationComponent())
                .with(new CollidableComponent(true))
                .with(new HealthComponent())
                .build();

        planteraEntity.getComponent(HealthComponent.class).getHealthDisplay().setTranslateY(-237);
        Enemy plantera = new Enemy(planteraEntity);

        final boolean[] switched = {false};
        planteraEntity.addComponent(new com.almasb.fxgl.entity.component.Component() {
            @Override
            public void onUpdate(double tpf) {
                HealthComponent hp = planteraEntity.getComponent(HealthComponent.class);
                AttributeComponent attr = planteraEntity.getComponent(AttributeComponent.class);
                if (!switched[0] && hp.getHealth() <= hp.getMaxHealth() / 2) {
                    animatedTexture.loopNoOverride(secondForm);
                    attr.setAttack(300);
                    attr.setDefense(40);
                    switched[0] = true;
                }
            }
        });

        planteraEntity.setProperty("Plantera", plantera);

        return planteraEntity;
    }
}
